/**
 * @filename:JobLogServiceImpl 2022-9-28
 * @project Wonder Java前后台分离框架  1.1.0
 * Copyright(c) 2022 Wonder Co. Ltd. 
 * All right reserved. 
 */
package com.wonder.admin.service.impl;

import com.wonder.admin.domain.JobLog;
import com.wonder.admin.mapper.JobLogDao;
import com.wonder.admin.service.JobLogService;
import com.wonder.admin.request.JobLogRequest;
import com.wonder.common.utils.StringUtils;
import com.wonder.common.utils.MessageUtils;
import com.wonder.common.common.AjaxMap;
import java.util.Map;
import java.util.HashMap;
import java.util.List;

import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import java.util.Date;

/**   
 * @Description:TODO(任务日志服务实现)
 *
 * @version: 1.1.0
 * @author: Wonder
 * 
 */
@Service
public class JobLogServiceImpl  extends ServiceImpl<JobLogDao, JobLog> implements JobLogService  {

    /**
     * 验证任务日志信息
     * 
     * @param JobLog信息
     * @return 结果
     */
    @Override
    public String validate(JobLog jobLog,String type){
    
    
       return "";
    }
    
    /**
	 * Description:任务日志分页返回
	 * @request:检索条件
	 * @page：分页参数
	 * @return:分页记录
	 */
	@Override 
	public IPage<JobLog> selectList(JobLogRequest request, Page<JobLog> page){
	    QueryWrapper<JobLog> queryWrapper = new QueryWrapper<>();
	    
	    if (null != request.getId()) {
	       queryWrapper.eq("id", request.getId());
	      }               
             
        if (StringUtils.hasText(request.getJob_name())) {
			    queryWrapper.like("job_name", request.getJob_name()); 
		    } 
             
        if (StringUtils.hasText(request.getModule())) {
			    queryWrapper.eq("module", request.getModule());
		    } 
             
	    if (null != request.getStatus()) {
	       queryWrapper.eq("status", request.getStatus());
	      }               
             
        if (request.getCreate_time() != null){
        	Date[] create_time=request.getCreate_time();
        	if(create_time.length>0)
        	  queryWrapper.ge("create_time", create_time[0]); 
        	if(create_time.length==2)
        	  queryWrapper.le("create_time",create_time[1]);      
        	
        }    
             
    
	    if(StringUtils.hasText(request.getOrder()) && StringUtils.hasText(request.getSort())){
	        if(request.getOrder().equals("desc")) {
	        		queryWrapper.orderByDesc(request.getSort());
	        }else {
	        		queryWrapper.orderByAsc(request.getSort());
	        }
	    }else{
			queryWrapper.orderByDesc("id");	
		}    		
		Page<JobLog> selectPage = this.baseMapper.selectPage(page, queryWrapper);

		return selectPage;
      }
        
	/**
	 * 导出数据
	 */
	@Override  
	public Map<String,Object> dumpdata(JobLogRequest request){
      Page<JobLog> page = new Page<JobLog>(request.getPage(), request.getLimit());
	  IPage<JobLog> list =selectList(request, page);
	  String header="编号,任务名称,任务组名,执行模块,调用目标字符串,日志信息,执行状态,异常信息,创建时间";
	  Map<String,Object> retMap=new HashMap<String, Object>();
	  retMap.put("header", StringUtils.split(header, ','));
	  retMap.put("filename", "任务日志.xlsx");
	  retMap.put("percentage", (int)list.getCurrent()*100/list.getPages());
	  retMap.put("data", list.getRecords());
	  return AjaxMap.success(retMap);
	}
	
	
	/**
     * 新增任务日志
     * 
     * @param jobLog 调度日志信息
     */
    @Override
    public void addJobLog(JobLog jobLog)
    {
        this.baseMapper.insert(jobLog);
    }
    
	
}